package mapReduce.demo01_wordCount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {

    IntWritable outValue = new IntWritable();

    int i=0;

    @Override
    protected void setup(Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        System.out.println("reducer=====setup");
    }

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

        System.out.println("第"+ ++i +"组的key为："+key.toString());

        //统计每个相同的key下，所有的value值的和
        int sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }

        outValue.set(sum);

        context.write(key,outValue);

    }

    @Override
    protected void cleanup(Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        System.out.println("reducer=====cleanup");
    }
}
